package com.ruoyi.web.controller.admin;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.xcx.Goods;
import com.ruoyi.system.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品管理Controller
 *
 * @author ruoyi
 * @date 2023-09-30
 */
@RestController
@RequestMapping("/douyin/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询商品管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }


    /**
     * 查询商品管理列表
     */
    @GetMapping("/getList")
    public AjaxResult getList(Goods goods)
    {
        return success(goodsService.selectGoodsList(goods));
    }
    /**
     * 导出商品管理列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @GetMapping(value = "/{goodId}")
    public AjaxResult getInfo(@PathVariable("goodId") String goodId)
    {
        Goods goods = goodsService.selectGoodsByGoodId(goodId);
        goods.setAge(JSONArray.parseArray(goods.getAge2()));
        return success(goods);
    }

    /**
     * 新增商品管理
     */
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        goods.setGoodId(IdUtils.fastSimpleUUID());
        goods.setAge2(goods.getAge().toString());
        int count=goodsService.insertGoods(goods);
        if(count>0){
            Goods goodsCache=new Goods();
            goodsCache.setGoodId(goods.getGoodId());
            goodsCache.setFirstPause(goods.getFirstPause());
            goodsCache.setSecondPause(goods.getSecondPause());
            goodsCache.setThirdPause(goods.getThirdPause());
            goodsCache.setGoodNum(goods.getGoodNum());
            goodsCache.setGoodsPrice(goods.getGoodsPrice());
            goodsCache.setBasicMemberPrice(goods.getBasicMemberPrice());
            goodsCache.setCpaBid(goods.getCpaBid());
            goodsCache.setStopType(goods.getStopType());
            goodsCache.setGoodSwitch(goods.getGoodSwitch());
            redisCache.setCacheObject(Constants.GOODS+goods.getGoodId(),goodsCache);
        }
        return toAjax(count);
    }

    /**
     * 修改商品管理
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        Goods old = goodsService.selectGoodsByGoodId(goods.getGoodId());
        goods.setAge2(goods.getAge().toString());
        int count=goodsService.updateGoods(goods);
        if(count>0){
            Goods goodsCache=new Goods();
            goodsCache.setGoodId(goods.getGoodId());
            goodsCache.setFirstPause(goods.getFirstPause());
            goodsCache.setSecondPause(goods.getSecondPause());
            goodsCache.setGoodNum(goods.getGoodNum());
            goodsCache.setGoodsPrice(goods.getGoodsPrice());
            goodsCache.setBasicMemberPrice(goods.getBasicMemberPrice());
            goodsCache.setCpaBid(goods.getCpaBid());
            goodsCache.setStopType(goods.getStopType());
            goodsCache.setGoodSwitch(goods.getGoodSwitch());
            redisCache.setCacheObject(Constants.GOODS+goods.getGoodId(),goodsCache);
        }
        return toAjax(count);
    }

    /**
     * 删除商品管理
     */
	@DeleteMapping("/{goodIds}")
    public AjaxResult remove(@PathVariable String[] goodIds)
    {
        int count=goodsService.deleteGoodsByGoodIds(goodIds);
        if(count>0){
            for (String goodId : goodIds) {
                redisCache.deleteObject(Constants.GOODS+goodId);
            }
        }
        return toAjax(count);
    }
}
